package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.help.RecheckingAddRuleHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.dao.RecheckingRuleDao;
import com.xbongbong.saas.domain.entity.RecheckingRuleEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.RecheckingRuleModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 重复规则model层
 * @author yanglei
 * @date 2022/11/7 16:50
 */
@Service("recheckingRuleModel")
@Slf4j
public class RecheckingRuleModelImpl implements RecheckingRuleModel {

    @Resource
    private RecheckingRuleDao recheckingRuleDao;
    @Resource
    private RecheckingAddRuleHelp recheckingAddRuleHelp;

    @Override
    public Integer insert(RecheckingRuleEntity entity) throws XbbException {
        entity.setDel(BasicConstant.ZERO);
        entity.setAddTime(DateUtil.getNow());
        entity.setUpdateTime(DateUtil.getNow());
        return recheckingRuleDao.insert(entity);
    }

    @Override
    public Integer update(RecheckingRuleEntity entity) throws XbbException {
        entity.setDel(BasicConstant.ZERO);
        entity.setUpdateTime(DateUtil.getNow());
        return recheckingRuleDao.update(entity);
    }

    @Override
    public List findEntitysWithoutSub(Map param) {
        return null;
    }

    @Override
    public Integer getEntitysCount(Map param) {
        return null;
    }

    @Override
    public List<RecheckingRuleEntity> findEntitys(Map<String,Object> param) {
        return recheckingRuleDao.findEntities(param);
    }

    @Override
    public RecheckingRuleEntity getRuleListByFormId(String corpid, Long formId,Integer businessType) throws XbbException {
        RecheckingRuleEntity entity = recheckingRuleDao.getRuleByFormId(corpid, formId,businessType);
        //如果该公司没有配置规则，拿默认的规则
        return Objects.isNull(entity) ? recheckingAddRuleHelp.initDefaultRecheckRuleEntity(businessType) : entity;
    }

    @Override
    public void insertBatch(List<RecheckingRuleEntity> list) throws XbbException {
        long now = DateUtil.getInt();
        for (RecheckingRuleEntity recheckingRuleEntity : list) {
            recheckingRuleEntity.setAddTime(now);
            recheckingRuleEntity.setUpdateTime(now);
            recheckingRuleEntity.setDel(BasicConstant.ZERO);
        }
        recheckingRuleDao.insertBatch(list);
    }

    @Override
    public void updateBatch(List<RecheckingRuleEntity> list, String corpid) throws XbbException {
        recheckingRuleDao.updateBatch(list,corpid);
    }

    @Override
    public void insertBatchByScript(List<RecheckingRuleEntity> list) throws XbbException {
        long now = DateUtil.getInt();
        for (RecheckingRuleEntity recheckingRuleEntity : list) {
            recheckingRuleEntity.setAddTime(now);
            recheckingRuleEntity.setUpdateTime(now);
            recheckingRuleEntity.setDel(BasicConstant.ZERO);
        }
        recheckingRuleDao.insertBatchByScript(list);
    }

    /**
     * @param corpid
     * @param businessType
     * @return RecheckingRuleEntity
     * @description 根据corpid, businessType判断业务是否开启常用查重规则
     * @author jiangren.li
     * @date 2022/11/15 9:51
     */
    @Override
    public Integer getNormalEnable(String corpid, Integer businessType) throws XbbException {
        RecheckingRuleEntity recheckingRule = recheckingRuleDao.getNormalEnable(corpid, businessType);
        if (Objects.nonNull(recheckingRule)) {
            return recheckingRule.getNormalEnable();
        }else {
            if (Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY)) {
                return BasicConstant.ZERO;
            }
        }
        return BasicConstant.ONE;
    }

    @Override
    public Integer save(RecheckingRuleEntity entity) throws XbbException {
        if (Objects.nonNull(entity.getId())){
            return update(entity);
        }
        return insert(entity);
    }

    @Override
    public List<RecheckingRuleEntity> getByBusinessType(Integer businessType, String corpid) {
        return recheckingRuleDao.getByBusinessType(corpid,businessType);
    }

    @Override
    public List<Long> getFormIdListByBusinessType(Integer businessType, String corpid) {
        return recheckingRuleDao.getFormIdListByBusinessType(corpid,businessType);
    }
}
